package com.design.patterns.list.proxy;

import com.design.patterns.list.proxy.dynamicproxy.ProxyFactory;
import com.design.patterns.list.proxy.staticproxy.InterfaceObject;
import com.design.patterns.list.proxy.staticproxy.ProxyObject;
import com.design.patterns.list.proxy.staticproxy.TargetObject;

/**
 * @author Wangzhiwen
 */
public class ProxyClient {

    private static void staticProxy(){

        System.out.println("未使用静态代理对象时的目标方法输出");

        InterfaceObject targetObject = new TargetObject();
        targetObject.operation();

        System.out.println("使用静态代理对象对目标方法增强");
        InterfaceObject proxyObject = new ProxyObject(targetObject);
        proxyObject.operation();
    }

    private static void jdkDynamicProxy(){
//        com.design.patterns.list.proxy.dynamicproxy.InterfaceObject proxy = (com.design.patterns.list.proxy.dynamicproxy.InterfaceObject)ProxyFactory.proxyInstance(new com.design.patterns.list.proxy.dynamicproxy.TargetObject());
        com.design.patterns.list.proxy.dynamicproxy.InterfaceObject proxy = ProxyFactory.proxyInstance(new com.design.patterns.list.proxy.dynamicproxy.TargetObject());
        proxy.targetMethod();
    }

    private static void cglibDynamicProxy(){
        com.design.patterns.list.proxy.cglibproxy.ProxyFactory proxyFactory = new com.design.patterns.list.proxy.cglibproxy.ProxyFactory(new com.design.patterns.list.proxy.cglibproxy.TargetObject());
        com.design.patterns.list.proxy.cglibproxy.TargetObject cglibProxyObject = (com.design.patterns.list.proxy.cglibproxy.TargetObject) proxyFactory.proxyInterface();
        cglibProxyObject.targetMethod();
    }

    public static void main(String[] args) {

        staticProxy();
        System.out.println("======================================");
        jdkDynamicProxy();
        System.out.println("======================================");
        cglibDynamicProxy();
    }

}
